Hack up GPX writer to prepare for GGZ.
authorRobert Lipe <robertlipe@gpsbabel.org>
Tue, 22 Sep 2015 01:56:41 +0000 (20:56 -0500)
committerRobert Lipe <robertlipe@gpsbabel.org>
Tue, 22 Sep 2015 01:56:41 +0000 (20:56 -0500)
optionally split output in gpx writer.
Keep a list of files we’ve created so that GGZ writer can turn around
and read them.

gpx.cc
gpx.h [new file with mode: 0644]

diff --git a/gpx.cc b/gpx.cc
index 4778b83a44bf0b73025d605ba864f4b23ee63f3c..7e2560d1c5dd585ee8111ee4820b337005684eba 100644 (file)
--- a/gpx.cc
+++ b/gpx.cc
 #include "cet_util.h"
 #include "garmin_fs.h"
 #include "garmin_tables.h"
+#include "gpx.h"
 #include "src/core/logging.h"
 #include "src/core/file.h"
 #include "src/core/xmlstreamwriter.h"
 #include "src/core/xmltag.h"
+#include "src/core/ziparchive.h"
 
 #include <QtCore/QXmlStreamReader>
 #include <QtCore/QRegExp>
@@ -62,9 +64,12 @@ static short_handle mkshort_handle;
 static QString link_url;
 static QString link_text;
 static QString link_type;
+static QString output_file_name;
+static QList<QString> gpx_files_written_;
 
 
 static char* snlen = NULL;
+static char* split = NULL;
 static char* suppresswhite = NULL;
 static char* urlbase = NULL;
 static route_head* trk_head;
@@ -1227,9 +1232,33 @@ gpx_rd_deinit(void)
   cur_tag = NULL;
 }
 
+// Secret accessor to return an array of file names that we have written.
+QList<QString> GetGpxFilesWritten() {
+  return gpx_files_written_;
+}
+
+// When we're writing multiple GPX files, get the output filename (which may
+// have a path component) in the current sequence.
+static QString GetOutFname(int seq) {
+  static const char* kNameToken = "__NUM__";
+
+  QString sequence = QString("%1").arg(seq);
+  QString s = output_file_name;
+  s.replace(kNameToken, sequence);
+  return s;
+}
+
 static void
-gpx_wr_init(const char* fname)
+gpx_wr_init(const char* ifname)
 {
+  QString fname = ifname;
+  if (output_file_name.isEmpty()) {
+    output_file_name = fname;
+    // Our first name is "one".
+    fname = GetOutFname(1);
+  }
+  gpx_files_written_.append(fname);
+
   mkshort_handle = NULL;
   oqfile = new gpsbabel::File(fname);
   oqfile->open(QIODevice::WriteOnly | QIODevice::Text);
@@ -1655,6 +1684,17 @@ gpx_waypt_pr(const Waypoint* waypointp)
   fs_xml* fs_gpx;
   garmin_fs_t* gmsd;   /* gARmIN sPECIAL dATA */
 
+  // If we're splitting files and the output buffer is approaching
+  // our split we close the file we were last writing and open a new
+  // one.
+  static const double kSplitPercent = 0.99;
+  if (oqfile->size() > atoi(split) * kSplitPercent) {
+    static int x = 2; // The first one is "one" automatically.
+    gpx_wr_deinit();
+    QString fn = GetOutFname(x++);
+    gpx_wr_init(CSTR(fn));
+  }
+
   writer->writeStartElement("wpt");
   writer->writeAttribute("lat", toString(waypointp->latitude));
   writer->writeAttribute("lon", toString(waypointp->longitude));
@@ -1929,6 +1969,10 @@ arglist_t gpx_args[] = {
     "snlen", &snlen, "Length of generated shortnames",
     "32", ARGTYPE_INT, "1", NULL, NULL
   },
+  {
+    "split", &split, "split wpts after this many bytes ",
+    "500000", ARGTYPE_INT, "1", NULL, NULL
+  },
   {
     "suppresswhite", &suppresswhite,
     "No whitespace in generated shortnames",
diff --git a/gpx.h b/gpx.h
new file mode 100644 (file)
index 0000000..f61cef8
--- /dev/null
+++ b/gpx.h
@@ -0,0 +1,21 @@
+/*
+    Access GPX data files.
+
+    Copyright (C) 2002-2015 Robert Lipe, gpsbabel.org
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
+
+ */
+QList<QString> GetGpxFilesWritten();